/*
 * This source code is part of TWaver 4.0
 *
 * Serva Software PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
 * Copyright 2002 - 2011 Serva Software. All rights reserved.
 */

package demo.table.explorer;

import javax.swing.*;
import javax.swing.table.DefaultTableCellRenderer;
import java.awt.*;
import java.text.NumberFormat;
 
public class FileSizeCellRenderer extends DefaultTableCellRenderer {

    private static NumberFormat numberFormat = NumberFormat.getInstance();
    public FileSizeCellRenderer(){
    	this.setHorizontalAlignment(JLabel.RIGHT);
    }
    
    public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) {
    	String sizeStr = "";
    	if (value instanceof Long) {
            sizeStr = format(((Long) value).longValue());
        }
    	return super.getTableCellRendererComponent(table, sizeStr, isSelected, hasFocus, row, column);
    }

    public static String format(long filesize) {
        if (filesize < 0) {
            return "";
        }else {
            return new StringBuffer(numberFormat.format(Math.round((double) filesize / 1024.0))).append(" KB ").toString();
        }
    }
}